package com.hiss.basic.starter.core.model;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1;

    private T data;

    private int code;

    private String message;

    public static final int SUCCESS = 200;

    public static final int FAILURE = 500;

    public static <T> R<T> success(int code, String message, T data) {
        R<T> res = new R<>();
        res.setCode(code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }

    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> success(T data) {
        return success("success", data);
    }

    public static <T> R<T> success(int code, T data) {
        return success(code, "success", data);
    }

    public static <T> R<T> success(String message, T data) {
        return success(SUCCESS, message, data);
    }

    public static <T> R<T> failure() {
        return failure("failure");
    }

    public static <T> R<T> failure(String message) {
        return failure(message, null);
    }

    public static <T> R<T> failure(String message, T data) {
        return failure(FAILURE, message, data);
    }

    public static <T> R<T> failure(int code, String message) {
        return failure(code, message, null);
    }

    public static <T> R<T> failure(int code, String message, T data) {
        R<T> res = new R<T>();
        res.setCode(code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }
}
